package com.shankungfu.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loopj.android.http.PersistentCookieStore;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shankungfu.MainApplication;
import com.shankungfu.R;
import com.shankungfu.bean.LoginDataBean;
import com.shankungfu.main.LogInActivity;
import com.shankungfu.main.MainActivity;
import com.shankungfu.main.PublishCommentActivity;
import com.shankungfu.main.RegisterActivity;
import com.shankungfu.main.UserCenterActivity;
import com.shankungfu.util.ToastUtil;
import com.shankungfu.view.CustomDialog;
import com.shankungfu.view.CustomDialog.Builder;

public class MyMenuFragment extends Fragment implements OnClickListener {
	
	private ImageView defaultUserPic;
	private Button dengluButton;
	private Button registerButton;
	private RelativeLayout log_in_layout;
	private RelativeLayout user_account_layout;
	private ImageView user_pic;
	private TextView username;
	private TextView volunteer_time;
	private Button faqiButton;
	private Button canjiaButton;
	private Button ganxingquButton;
	private RelativeLayout pickItem;
	private RelativeLayout browseFind;
	private RelativeLayout publicInformation;
	private RelativeLayout setting;
	private RelativeLayout message_ll;
	private RelativeLayout log_out_ll;
	private ImageView project_icon;
	private ImageView browse_icon;
	private ImageView message_icon;
	private ImageView setting_icon;
	private ImageView logout_icon;
	private TextView project_icon_text;
	private TextView browse_icon_text;
	private TextView message_icon_text;
	private TextView setting_icon_text;
	private TextView logout_icon_text;
	private ImageLoader imageLoader;

	private LinearLayout pickitem_title_text;


	private TextView head_text;


	private ImageView head_refresh;
	private LinearLayout user_message;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.menu_layout, null);
		imageLoader = ImageLoader.getInstance();
		initView(v);
		return v;
	}
	
	private void initView(View v) {
		defaultUserPic = (ImageView) v.findViewById(R.id.default_user_pic);
		user_pic = (ImageView) v.findViewById(R.id.user_pic);
		dengluButton = (Button) v.findViewById(R.id.denglu_button);
		registerButton = (Button) v.findViewById(R.id.register_button);
		username = (TextView) v.findViewById(R.id.username);
		volunteer_time = (TextView) v.findViewById(R.id.volunteer_time); //"志愿时：11225小时"
		faqiButton = (Button) v.findViewById(R.id.faqi_button); //20\n发起
		canjiaButton = (Button) v.findViewById(R.id.canjia_button); //10\n参加
		ganxingquButton = (Button) v.findViewById(R.id.ganxingqu_button); //100\n感兴趣
		
		log_in_layout = (RelativeLayout) v.findViewById(R.id.log_in_layout);
		user_account_layout = (RelativeLayout) v.findViewById(R.id.user_account_layout);
		
		message_ll = (RelativeLayout) v.findViewById(R.id.message_ll);
		log_out_ll = (RelativeLayout) v.findViewById(R.id.log_out);
		
		pickItem = (RelativeLayout) v.findViewById(R.id.pick_item);
		browseFind = (RelativeLayout) v.findViewById(R.id.browse_find);
		publicInformation = (RelativeLayout) v.findViewById(R.id.public_information);
		setting = (RelativeLayout) v.findViewById(R.id.setting);
		
		project_icon = (ImageView) v.findViewById(R.id.project_icon);
		browse_icon = (ImageView) v.findViewById(R.id.browse_icon);
		message_icon = (ImageView) v.findViewById(R.id.message_icon);
		setting_icon = (ImageView) v.findViewById(R.id.setting_icon);
		logout_icon = (ImageView) v.findViewById(R.id.logout_icon);
		
		project_icon_text = (TextView) v.findViewById(R.id.project_icon_text);
		browse_icon_text = (TextView) v.findViewById(R.id.browse_icon_text);
		message_icon_text = (TextView) v.findViewById(R.id.message_icon_text);
		setting_icon_text = (TextView) v.findViewById(R.id.setting_icon_text);
		logout_icon_text = (TextView) v.findViewById(R.id.logout_icon_text);
		
		
		
		//获取Activity中的东西。
		pickitem_title_text = (LinearLayout) getActivity().findViewById(R.id.pickitem_title_text);
		head_text = (TextView) getActivity().findViewById(R.id.head_text);
		head_refresh = (ImageView) getActivity().findViewById(R.id.head_refresh);
		
		pickItem.setOnClickListener(this);
		browseFind.setOnClickListener(this);
		publicInformation.setOnClickListener(this);
		setting.setOnClickListener(this);
		log_out_ll.setOnClickListener(this);
		message_ll.setOnClickListener(this);
		
		defaultUserPic.setOnClickListener(this);
		dengluButton.setOnClickListener(this);
		registerButton.setOnClickListener(this);
		faqiButton.setOnClickListener(this);
		canjiaButton.setOnClickListener(this);
		ganxingquButton.setOnClickListener(this);
		
		
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		showUser();
	}

	private void showUser() {
		if (MainApplication.isUserIsLogin()) {
			message_ll.setVisibility(View.VISIBLE);
			log_out_ll.setVisibility(View.VISIBLE);
			
			LoginDataBean userBean = MainApplication.userBean.getLoginData();
			user_pic.setOnClickListener(this);
			username.setText(userBean.getUser_name());
			volunteer_time.setText("志愿时："+userBean.getUser_services_time()+"小时");
			faqiButton.setText(userBean.getMy_start()+"\n发起");
			canjiaButton.setText(userBean.getMy_join()+"\n参加");
			ganxingquButton.setText(userBean.getMy_insterest()+"\n感兴趣");
			imageLoader.displayImage(userBean.getAtatar_url(), user_pic, MainApplication.getOptions(), null);
			log_in_layout.setVisibility(View.GONE);
			user_account_layout.setVisibility(View.VISIBLE);
		} else {
			message_ll.setVisibility(View.GONE);
			log_out_ll.setVisibility(View.GONE);
			log_in_layout.setVisibility(View.VISIBLE);
			user_account_layout.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		Fragment newContent = null;
		switch (v.getId()) {
			
		case R.id.pick_item:
			
			changeBackground(R.id.pick_item);
			isPickItemView();
			newContent = new PickItemFragment();
			break;
		case R.id.browse_find:
			changeBackground(R.id.browse_find);
			notPickItemView();
			head_text.setText("浏览发现");
			newContent = new BrowseFindFragment();
			break;
//		case R.id.public_information:
//			notPickItemView();
//			head_text.setText("公益资讯");
//			newContent = new PublicInformationFragment();
//			break;
		case R.id.message_ll:
			changeBackground(R.id.message_ll);
			notPickItemView();
			head_text.setText("消息");
			newContent = new MessageFragment();
			break;
		case R.id.setting:
			changeBackground(R.id.setting);
			notPickItemView();
			head_refresh.setVisibility(View.GONE);
			head_text.setText("设置");
			newContent = new SettingFragment();
			break;
//		case R.id.logOut:
//			newContent = new ThreeFragment();
//			break;
			
		case R.id.denglu_button:
			Intent dengluIntent = new Intent(getActivity(),LogInActivity.class);
			startActivity(dengluIntent);
			return;
		case R.id.register_button:
			Intent registerIntent = new Intent(getActivity(),RegisterActivity.class);
			startActivity(registerIntent);
			return;
		case R.id.user_pic:
		case R.id.faqi_button:
			startUserCenter(UserCenterActivity.FAQI);
			return;
		case R.id.canjia_button:
			startUserCenter(UserCenterActivity.CANJIA);
			return;
		case R.id.ganxingqu_button:
			startUserCenter(UserCenterActivity.GANXINGQU);
			return;
		case R.id.log_out:
			changeBackground(R.id.log_out);
			showLogoutDailog();
			break;
		}
		if (newContent != null)
			switchFragment(newContent);
		
	}

	private void startUserCenter(int categroy) {
		if (MainApplication.isUserIsLogin()) {
			Intent intent = new Intent(getActivity(),UserCenterActivity.class);
			intent.putExtra(UserCenterActivity.USER_CATEGORY, categroy);
			startActivity(intent);
		} else {
			ToastUtil.showMessageText(getActivity(), "请先登录");
			Intent intent = new Intent(getActivity(), LogInActivity.class);
			startActivity(intent);
		}
	}

	private void notPickItemView() {
		head_refresh.setVisibility(View.VISIBLE);
		pickitem_title_text.setVisibility(View.GONE);
		head_text.setVisibility(View.VISIBLE);
	}
	
	private void isPickItemView() {
		head_refresh.setVisibility(View.VISIBLE);
		pickitem_title_text.setVisibility(View.VISIBLE);
		head_text.setVisibility(View.GONE);
	}

	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;
		if (getActivity() instanceof MainActivity) {
			MainActivity ma = (MainActivity) getActivity();
			ma.switchContent(fragment);
		}
	}
	
	private void changeBackground(int id){
		pickItem.setBackgroundResource(R.drawable.menu_item_selector);
		browseFind.setBackgroundResource(R.drawable.menu_item_selector);
		message_ll.setBackgroundResource(R.drawable.menu_item_selector);
		setting.setBackgroundResource(R.drawable.menu_item_selector);
		log_out_ll.setBackgroundResource(R.drawable.menu_item_selector);
		
		project_icon.setBackgroundResource(R.drawable.project_icon_selector);
		browse_icon.setBackgroundResource(R.drawable.browse_icon_selector);
		message_icon.setBackgroundResource(R.drawable.message_icon_selector);
		setting_icon.setBackgroundResource(R.drawable.setting_icon_selector);
		logout_icon.setBackgroundResource(R.drawable.logout_icon_selector);
		
		project_icon_text.setTextColor(getActivity().getResources().getColor(R.color.menu_text_color_selector));
		browse_icon_text.setTextColor(getActivity().getResources().getColor(R.color.menu_text_color_selector));
		message_icon_text.setTextColor(getActivity().getResources().getColor(R.color.menu_text_color_selector));
		setting_icon_text.setTextColor(getActivity().getResources().getColor(R.color.menu_text_color_selector));
		logout_icon_text.setTextColor(getActivity().getResources().getColor(R.color.menu_text_color_selector));
		
		switch (id) {
		case R.id.pick_item:
			pickItem.setBackgroundResource(R.drawable.menu_item_bg_true);
			project_icon.setBackgroundResource(R.drawable.project_icon_true);
			project_icon_text.setTextColor(Color.WHITE);
			break;
		case R.id.browse_find:
			browseFind.setBackgroundResource(R.drawable.menu_item_bg_true);
			browse_icon.setBackgroundResource(R.drawable.browse_icon_true);
			browse_icon_text.setTextColor(Color.WHITE);
			break;
		case R.id.message_ll:
			message_ll.setBackgroundResource(R.drawable.menu_item_bg_true);
			message_icon.setBackgroundResource(R.drawable.message_icon_true);
			message_icon_text.setTextColor(Color.WHITE);
			break;
		case R.id.setting:
			setting.setBackgroundResource(R.drawable.menu_item_bg_true);
			setting_icon.setBackgroundResource(R.drawable.setting_icon_true);
			setting_icon_text.setTextColor(Color.WHITE);
			break;
		case R.id.log_out:
			log_out_ll.setBackgroundResource(R.drawable.menu_item_bg_true);
			logout_icon.setBackgroundResource(R.drawable.logout_icon_true);
			logout_icon_text.setTextColor(Color.WHITE);
			break;

		default:
			break;
		}
	}
	
	private void showLogoutDailog(){
		CustomDialog.Builder builder = new Builder(getActivity());
		builder.setMessage("您确定要退出当前账户的登录吗?");
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				MainApplication.userBean = null;
				MainApplication.setUserIsLogin(false);
				showUser();
				PersistentCookieStore cookieStore = new PersistentCookieStore(MainApplication.context);
				cookieStore.clear();
			}
		});
		builder.create().show();
	}

}
